﻿// (c) Norbert Huffschmid
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.

using System;
using System.Collections.Specialized;
using System.Reflection;
using log4net;
using NProtocol;
using NProtocol.Codec;
using ShoutBox.Core;
using ShoutBox.Core.Resources;

namespace ShoutBox
{
    public class WpfViewModel : ViewModel
    {
        /// <summary>
        /// Fires when a shutdown request has been received
        /// </summary>
        public event EventHandler ShutDownRequestReceived;


        /// <summary>
        /// Gets the shoutbox instance
        /// </summary>
        public override IShoutBox ShoutBox
        {
            get { return this.shoutBoxProxy; }
        }

        /// <summary>
        /// Gets the platform adapter
        /// </summary>
        public override IPlatformAdapterAudioIn PlatformAdapter
        {
            get { return this.platformAdapter; }
        }


        /// <summary>
        /// Gets the shoutbox version information
        /// </summary>
        public override string VersionInfo
        {
            get
            {
                Assembly assembly = Assembly.GetExecutingAssembly();
                AssemblyName asmName = new AssemblyName(assembly.FullName);
                Version version = asmName.Version;

                return Strings.AppName + " - " + Strings.Version + ": " + version.Major + "." + version.Minor + "." + version.Build;
            }
        }


        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private WindowsWaveInPlatformAdapter platformAdapter;
        private ShoutBoxProxy shoutBoxProxy;


        /// <summary>
        /// Constructor
        /// </summary>
        public WpfViewModel()
        {
            log.Info("Constructor");

            try
            {
                this.shoutBoxProxy = new ShoutBoxProxy();
                this.shoutBoxProxy.SessionMembers.CollectionChanged += new NotifyCollectionChangedEventHandler(SessionMembers_CollectionChanged);
                this.shoutBoxProxy.ShutDownRequestReceived += new EventHandler(shoutBoxProxy_ShutDownRequestReceived);
                this.shoutBoxProxy.Refresh();

                this.platformAdapter = new WindowsWaveInPlatformAdapter();
                this.platformAdapter.AudioDataAvailable +=
                    new EventHandler<RealTimeDataEventArgs>(platformAdapter_AudioDataAvailable);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + ex.StackTrace);

                throw ex;
            }
        }


        protected virtual void OnShutRequestReceived()
        {
            if (this.ShutDownRequestReceived != null)
                this.ShutDownRequestReceived(this, EventArgs.Empty);
        }


        private void shoutBoxProxy_ShutDownRequestReceived(object sender, EventArgs e)
        {
            // make sure that view is notified on its UI thread
            this.uiDispatcher.BeginInvoke(new Action(delegate()
            {
                OnShutRequestReceived();
            }), null);
        }
    }
}
